<?php

/**
 * Description of Model_acl
 *
 * @author Meta Sanjaya
 */
class Model_acl extends MY_Model {

    /**
     *
     * @var Model_module
     */
    var $model_module;

    public function __construct() {
        parent::__construct();
        $this->model_module = $this->load_model('model_module');
    }

    /**
     *
     * @param Obj_Group $group
     * @return Obj_Acl
     */
    function get_acl($group) {
        $acl = new Obj_Acl();
        $acl->group = $group;

        if ($group->id == Obj_Group::$admin) {
            $query = $this->db->query("select * from `" . DATABASE_ERP . "`.`module`");

            foreach ($query->result() as $module) {
                if($this->_cek($module->id, $acl->module)) {
                    $acl->module[] = $this->model_module->get_module($module->id);
                }
            }
        } else {
            $query = $this->db->query("select * from `" . DATABASE_ERP . "`.`acl` where `group` = " . $group->id);

            foreach ($query->result() as $row) {
                if($this->_cek($row->module, $acl->module)) {
                    $acl->module[] = $this->model_module->get_module($row->module);
                }
            }
        }

        return $acl;
    }

    function _cek($id, $modules) {
        foreach ($modules as $m) {
            if ($m->id == $id) {
                return false;
            }

            if (!$this->_cek($id, $m->child)) {
                return false;
            }
        }

        return true;
    }
}

?>
